package cn.wolfcode.trip.admin.web.controller;

import cn.wolfcode.trip.base.domain.Region;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.IRegionService;
import cn.wolfcode.trip.base.util.JSONResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;


@Controller
@RequestMapping("/region")
public class RegionController {

    @Autowired
    private IRegionService regionService;

    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    //地区的保存或新增
    public Object saveOrUpdate(Region region){
        regionService.saveOrUpdate(region);
        return new JSONResult();
    }

    @RequestMapping("/list")
    //地区列表
    public String list(Model model, @ModelAttribute("qo") QueryObject qo) {
        return "/region/list";
    }


    @RequestMapping("/getRegionByParentId")
    @ResponseBody
    //地区列表树状折叠需要的数据
    public Object getRegionByParentId(Long ParentId) {

        List<Region> list = regionService.getRegionByParentId(ParentId);
        List<HashMap<String, Object>> mapList = new ArrayList<HashMap<String, Object>>();

        for (Region region : list) {
            HashMap<String, Object> map = new HashMap<String, Object>();
            map.put("id", region.getId());
            map.put("text", region.getName());

            if(region.getState()==Region.STATE_HOT){
                map.put("tags", new String[]{"热门"});
            }
            //给该节点添加一个展开图标
            map.put("lazyLoad", true);
            map.put("ParentId", region.getParentId());
            map.put("json", region.getJsonString());

            mapList.add(map);
        }
        return mapList;
    }

}
